package com.fh.controller.information.classe;

import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.service.information.classe.ClasseService;
import com.fh.util.AppUtil;
import com.fh.util.Const;
import com.fh.util.DateUtil;
import com.fh.util.PageData;

/**
 * 类名称：NoticeController
 * 创建人：FH
 * 创建时间：2014年12月1日
 * 
 * @version
 */
@Controller
@RequestMapping(value = "/classe")
public class ClasseController extends BaseController {
	@Resource(name = "classeService")
	private ClasseService classeService;

	/**
	 * 列表
	 */
	@RequestMapping(value = "/list")
	public ModelAndView list(HttpSession session, Page page) throws Exception {
		logBefore(logger, "班级列表");
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		try {
			pd = this.getPageData();

			// 检索条件================================
			String search = pd.getString("search");
			if ((null != search) && !"".equals(search)) {
				search = search.trim();
				pd.put("studentpiny", search);
			}
			// 检索条件================================

			page.setPd(pd);
			List<PageData> List = classeService.list(page);

			mv.setViewName("information/school/classe/classe_list");
			mv.addObject("List", List);
			mv.addObject("pd", pd);
			mv.addObject(Const.SESSION_QX, this.getHC()); // 按钮权限
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}

		return mv;
	}

	/**
	 * 
	 * TODO 方法作用：跳转至详细页面
	 * 
	 * @return
	 * @Author: 孙超文
	 * @Date: 2015-7-2 上午9:45:53
	 */
	@RequestMapping(value = "/goFind")
	public ModelAndView goFind() {
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			pd = classeService.findById(pd);
			mv.setViewName("information/school/classe/classe_from");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		return mv;
	}

	/**
	 * 去新增页面
	 */
	@RequestMapping(value = "/goAdd")
	public ModelAndView goAdd() {
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			mv.setViewName("information/school/classe/classe_edit");
			mv.addObject("msg", "save");
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		return mv;
	}

	/**
	 * 去修改页面
	 */
	@RequestMapping(value = "/goEdit")
	public ModelAndView goEdit() {
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {

			pd = classeService.findById(pd);

			mv.setViewName("information/school/classe/classe_edit");
			mv.addObject("msg", "edit");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		return mv;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/edit")
	public ModelAndView edit(PrintWriter out) throws Exception {
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("modifier", getSessionUserid()); // 修改人
		pd.put("modifydate", DateUtil.getTime()); // 修改时间
		classeService.edit(pd);

		mv.addObject("msg", "success");
		mv.setViewName("save_result");
		return mv;
	}

	/**
	 * 保存公司公告
	 */
	@RequestMapping(value = "/save")
	public ModelAndView save(PrintWriter out) throws Exception {
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("id", get32UUID());
		// 公司简拼为null，系统设置首字母
		pd.put("schoolid", getSessionCompanyid());
		pd.put("creator", getSessionUserid()); // 新增人
		pd.put("modifier", getSessionUserid()); // 修改人
		pd.put("createdate", DateUtil.getTime()); // 新增时间
		pd.put("modifydate", DateUtil.getTime()); // 修改时间
		classeService.save(pd);
		mv.addObject("msg", "success");
		mv.setViewName("save_result");
		return mv;
	}

	/**
	 * 物理删除
	 */
	@RequestMapping(value = "/delete")
	public void delete(PrintWriter out) throws Exception {
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		try {
			pd = this.getPageData();
			classeService.delete(pd);
			out.write("success");
			out.close();
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}

	}

	/**
	 * 
	 * TODO 方法作用：班级列表All
	 * 
	 * @return
	 * @Author: 孙超文
	 * @Date: 2015-10-20 上午10:18:33
	 */
	@ResponseBody
	@RequestMapping(value = "/classeTree")
	public Object classeTree() {
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();

		List<PageData> list = new ArrayList<PageData>();
		try {
			pd.put("schoolid", getSessionCompanyid());
			list = classeService.listAll(pd);

		} catch (Exception e) {
			e.printStackTrace();
		}
		map.put("result", "success");
		map.put("data", list);
		return AppUtil.returnObject(new PageData(), map);
	}

	/* ===========================================导入导出============================== */

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(format, true));
	}

	/* ===============================权限================================== */
	public Map<String, String> getHC() {
		Subject currentUser = SecurityUtils.getSubject(); // shiro管理的session
		Session session = currentUser.getSession();
		return (Map<String, String>) session.getAttribute(Const.SESSION_QX);
	}
	/* ===============================权限================================== */
}
